package com.kplzj.maptest.map.customView;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.Log;

import com.kplzj.maptest.map.customView.MapBean;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class XmlDataParser {

    private List<Float> floatList;
    private List<String> colorList;
    private float scale = 1F;
    List<MapBean> mapBeanList;


    public List<Path> parseXmlFromAssets(Context context, String filename) {

        colorList = new ArrayList<>();
        mapBeanList = new ArrayList<>();

        List<Path> paths = new ArrayList<>();
        Path path = null;
        PointF minPoint = new PointF(Float.MAX_VALUE, Float.MAX_VALUE);

        try {
            AssetManager assetManager = context.getAssets();
            InputStream inputStream = assetManager.open(filename);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(inputStream);
            Element rootElement = doc.getDocumentElement();
            NodeList areaNodes = rootElement.getElementsByTagName("Area");

//            //网络获取解析
//            try {
//                // 创建DocumentBuilderFactory实例
//                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//                // 创建DocumentBuilder实例
//                DocumentBuilder builder = factory.newDocumentBuilder();
//                // 使用StringReader来读取字符串中的XML内容
//                StringReader stringReader = new StringReader(filename);
//                // 创建InputSource以从StringReader中读取
//                InputSource inputSource = new InputSource(stringReader);
//                 //解析InputSource来创建Document实例
//                Document doc = builder.parse(inputSource);
//                // 获取文档的根元素
//                Element rootElement = doc.getDocumentElement();
//                // 获取所有名为"Area"的节点
//                NodeList areaNodes = rootElement.getElementsByTagName("Area");


                Element areaElement;
//          Log.e("TagABN","areaNodes集合长度:"+areaNodes.getLength());
            for (int i = 0; i < areaNodes.getLength(); i++) {
                areaElement = (Element) areaNodes.item(i);
                String areaNum = areaElement.getAttribute("NUM");
                String areaName = areaElement.getAttribute("Name");
                String areaID = areaElement.getAttribute("ID");
//              Log.e("TagAB","Area NUM: " + areaNum + ", Name: " + areaName + ", ID: " + areaID);
                MapBean mapBean = new MapBean();
                mapBean.setName(areaName);
                mapBean.setId(areaID);
                mapBean.setNum(areaNum);

                List<MapBean.SubAreasbean> subAreaList = new ArrayList<>();
                NodeList subAreaNodes = areaElement.getElementsByTagName("SubArea");
//                Log.e("TagAB","subAreaNodes集合长度:"+subAreaNodes.getLength());
                for (int j = 0; j < subAreaNodes.getLength(); j++) {

//                        Log.e("TagAB","subAreaNodes:"+j);

                    Element subAreaElement = (Element) subAreaNodes.item(j);
                    String type = subAreaElement.getAttribute("Type");
                    String alpha = subAreaElement.getAttribute("Alpha");
                    String color = subAreaElement.getAttribute("Color");

                    MapBean.SubAreasbean subAreasbean = new MapBean.SubAreasbean();
                    colorList.add(color);

                    subAreasbean.setColor(color);
                    subAreasbean.setType(type);
                    subAreasbean.setAlpha(alpha);
                    subAreaList.add(subAreasbean);
                    List<MapBean.SubAreasbean.Pointsbean> pointsbeanList = new ArrayList<>();
//                    if (path != null) {
//                        paths.add(path);
//                        path = null;
//                    }
//                    Log.e("TagAB","SubArea Type: " + type + ", Alpha: " + alpha + ", Color: " + color+"   subAreaNodes.getLength():"+subAreaNodes.getLength());

                    NodeList ptNodes = subAreaElement.getElementsByTagName("Pt");
                    for (int k = 0; k < ptNodes.getLength(); k++) {
                        Element ptElement = (Element) ptNodes.item(k);
                        float x = Float.parseFloat(ptElement.getAttribute("x"))* scale;
                        float y = Float.parseFloat(ptElement.getAttribute("y"))* scale;
                        float z = Float.parseFloat(ptElement.getAttribute("z"))* scale;
                        MapBean.SubAreasbean.Pointsbean pointsbean = new MapBean.SubAreasbean.Pointsbean();


                        floatList = new ArrayList<>();
                        minPoint.x = Math.min(minPoint.x, x);
                        minPoint.y = Math.min(minPoint.y, y);
                        floatList.add(minPoint.x);
                        floatList.add(minPoint.y);

//                        Log.e("TagAB","下minPoint.x :"+minPoint.x+"   minPoint.y ："+minPoint.y);
                        pointsbean.setX(x);
                        pointsbean.setY(y);
                        pointsbean.setZ(z);
                        pointsbeanList.add(pointsbean);

                        path = new Path();
                    }

                    subAreasbean.setPoints(pointsbeanList); // 设置SubArea的Pt集合
                    subAreaList.add(subAreasbean); // 将SubArea实例添加到集合中
                }

                mapBean.setSubAreas(subAreaList); // 设置Area的SubArea集合
                mapBeanList.add(mapBean);// 将Area实例添加到集合中
//              Log.e("TagAB","floatList:"+);
            }

            if (path != null) {
                path.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paths;
    }

    public List<Float> getFloatList() {

        return floatList;
    }

    public List<String> getSubAreaColor() {

        return colorList;
    }

    public List<MapBean> getMapBeanList() {

        return mapBeanList;
    }

}